<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Icon as RM;
use app\admin\validate\IconValidate;


class Icon extends Common
{
    private $noCheckAction = [];//不需要验证的方法
    
    public function initialize(){
        parent::initialize();
        $this->doCheckAuth($this->noCheckAction);
    }
    
    /**
    * 图标列表
    */
    public function index()
    {
        $model = new RM();
        $data = $model->_list();
        $count = $model->_count();
        
        
        $this->assign([
            'list' => $data['list'],
            'page' => $data['page'],
            'count' => $count,
        ]);
        
        return $this->fetch();
    }

    /**
    * 图标添加
    */
    public function add(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $validate = new IconValidate();
            if(!$validate->scene(IconValidate::SCENE_ADD)->check($request)){
                die(outputError($validate->getError()));
            }
            
            $mode = new RM();
            $ret = $mode->_create($request);
            if($ret){
                die(outputSuccess([], '添加成功'));
            }else{
                die(outputError($mode->getError()));
            }
        }else{
            
            return $this->fetch();
        }
        
    }
    
    
    /**
    * 图标编辑
    */
    public function edit(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $validate = new IconValidate();
            if(!$validate->scene(IconValidate::SCENE_MODIFY)->check($request)){
                die(outputError($validate->getError()));
            }
            
            $mode = new RM();
            $ret = $mode->_modify($request);
            if($ret){
                die(outputSuccess([], '编辑成功'));
            }else{
                die(outputError($mode->getError()));
            }
        }else{
            $id = (int)$this->request->get('id');

            $model = new RM();
            $data = $model->_findOne($id);
           
            $this->assign([
                'data' => $data,
            ]);
            return $this->fetch();
        }
    }
    
    /**
    * 删除图标
    */
    public function del(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $validate = new IconValidate();
            if(!$validate->scene(IconValidate::SCENE_REMOVE)->check($request)){
                die(outputError($validate->getError()));
            }
            
            $mode = new RM();
            $id = (int)$request['id'];
            $ret = $mode->_remove($id);
            if($ret){
                die(outputSuccess([], '删除成功'));
            }else{
                die(outputError($mode->getError()));
            }
        }else{
            die(outputError('请求错误'));
        }
    }
}
